/*
 * Copyright 2014 Red Hat, Inc.
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Apache License v2.0 which accompanies this distribution.
 *
 *  The Eclipse Public License is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  The Apache License v2.0 is available at
 *  http://www.opensource.org/licenses/apache2.0.php
 *
 *  You may elect to redistribute this code under either of these licenses.
 */

package com.lostad.app.vert.api.verticle;
import com.lostad.app.vert.api.entity.Product;
import com.lostad.app.vert.core.bean.MsgCodec;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;


public class ProductSender extends AbstractVerticle{
  public static final String LIST_PRODUCTS = "/product/list";
  public static final String SAVE_PRODUCT   = "/product/save";
  public static final String LOAD_PRODUCT   = "/product/load/:id";
  Router router = null;
  public ProductSender(Router router) {
	this.router = router;
}

@Override
  public void start() {
	router.route().handler(BodyHandler.create());
    router.route(LOAD_PRODUCT).handler(this::loadHandle);
    router.route(LIST_PRODUCTS).handler(this::listHandle);
    router.route(SAVE_PRODUCT).handler(this::saveHandle);
  }

  private void loadHandle(RoutingContext routingContext) {
    final String productID = routingContext.request().getParam("id");
    HttpServerResponse response = routingContext.response().setChunked(true);
    response.setChunked(true);
    //response.putHeader("content-type", "application/json").end("{\"msg\":\"Hello\"}");
    vertx.eventBus().<String>send(LOAD_PRODUCT,productID, result -> {
        if (result.succeeded()) {
        	response.setStatusCode(200).write(result.result().body()).end();
        } else {
        	response.setStatusCode(500).write(result.cause().toString()).end();
        }
      });	  
  }

  private void listHandle(RoutingContext routingContext) {
    HttpServerResponse response = routingContext.response().setChunked(true);
    vertx.eventBus().<String>send(LIST_PRODUCTS, "", result -> {
        if (result.succeeded()) {
        	response.setStatusCode(200).write(result.result().body()).end();
        } else {
        	response.setStatusCode(500).write(result.cause().toString()).end();
        }
      });
  }

  private void saveHandle(RoutingContext routingContext) {
	HttpServerRequest req = routingContext.request();
	//这里可以优化为自动注入
    Product p = new Product();
    p.setDescription( req.getParam("remark"));
    p.setProductId(req.getParam("id"));
    
//    routingContext.response().putHeader("content-type", "application/json").end(arr.encodePrettily());
    HttpServerResponse response = routingContext.response().setChunked(true);
    vertx.eventBus().registerDefaultCodec(Product.class, new MsgCodec());  
    vertx.eventBus().<String>send(SAVE_PRODUCT,p, result -> {
        if (result.succeeded()) {
        	response.setStatusCode(200).write(result.result().body()).end();
        } else {
        	response.setStatusCode(500).write(result.cause().toString()).end();
        }
      });
  }

  
  private void sendError(int statusCode, HttpServerResponse response) {
    response.setStatusCode(statusCode).end();
  }



}
